//
//  NSString+Ext.m
//  WanLian
//
//  Created by 123456 on 2018/12/9.
//  Copyright © 2018 wanlian. All rights reserved.
//

#import "NSString+Ext.h"
#define SIZE_UNIT_KB (1 * 1024)
#define SIZE_UNIT_MB (1 * 1024 * 1024)
#define SIZE_UNIT_GB (1 * 1024 * 1024 * 1024)

@implementation NSString (Ext)
- (NSString *)filterExtension {
    if (self.length > 0) {
        NSString *res = nil;
        NSString *pathExtensin = self.pathExtension;
        if (pathExtensin.length > 0) {
            res = [self stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", pathExtensin] withString:@""];
            return res;
        }
    }
    return self;
}
+ (NSURL *)createRightDocumentFileURL:(NSURL *)oriURL {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *originPath = oriURL.path;
    if (![originPath containsString:documentPath]) {
        NSRange documentRange = [originPath rangeOfString:@"Documents"];
        if (documentRange.location != NSNotFound) {
            NSString *relatePath = [originPath substringFromIndex:documentRange.location];
            NSString *curPath = [documentPath stringByReplacingOccurrencesOfString:@"Documents" withString:relatePath];
            NSURL *curURL = [NSURL fileURLWithPath:curPath];
            return curURL;
        }
    }
    return oriURL;
}
+ (NSString *)formateSizeWithBytes:(unsigned long long)fileTotalBytes {
    NSString *formateStr = nil;
    if (fileTotalBytes <= 0) {
        return @"0KB";
    }
    if (fileTotalBytes < SIZE_UNIT_KB) {
        formateStr = [NSString stringWithFormat:@"%lluB", fileTotalBytes];
    } else if (fileTotalBytes < SIZE_UNIT_MB ) {
        formateStr = [NSString stringWithFormat:@"%.1lfKB",  (double)(fileTotalBytes / SIZE_UNIT_KB)];
    } else if (fileTotalBytes < SIZE_UNIT_GB) {
        formateStr = [NSString stringWithFormat:@"%.1lfMB", (double)fileTotalBytes / SIZE_UNIT_MB];
    } else {
        formateStr = [NSString stringWithFormat:@"%.1lfGB", (double)fileTotalBytes / SIZE_UNIT_GB];
    }
    return formateStr;
}
+ (NSString *)formateSizeWithKBytes:(unsigned long long)fileTotalBytes {
    NSString *formateStr = nil;
    if (fileTotalBytes <= 0) {
        return @"0KB";
    }
    fileTotalBytes = fileTotalBytes * 1024;
    formateStr = [self formateSizeWithBytes:fileTotalBytes];
    return formateStr;
}

+(NSInteger)getNowTimestampWithFormater:(NSString *)formater{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:formater]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间
    WLLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    WLLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
    return timeSp;
}
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    return timeSp;
}
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
    return confromTimespStr;
}

+ (NSString *)getNowStrWithFormater:(NSString *)formater {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:formater]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间
    return [formatter stringFromDate:datenow];
}
- (id)jsonObject {
    NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
    return jsonObject;
}
@end
